package com.deyuanyun.pic.app.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.deyuanyun.pic.common.util.CommonUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deyuanyun.pic.app.service.PigService;
import com.deyuanyun.pic.common.dto.BasicDataByFind;
import com.deyuanyun.pic.common.dto.Result;
import com.deyuanyun.pic.common.exception.InterfaceCode;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ResponseUtil;
import com.deyuanyun.pic.common.util.UploadUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.pig.domain.PigResults;
import com.deyuanyun.pic.pig.domain.PigTaskPoint;
import com.deyuanyun.pic.service.base.AttachmentService;
import com.github.pagehelper.PageHelper;

@Controller
@RequestMapping("/app")
public class PigController {
	@Resource
	private PigService pigService;

	@Resource
	private AttachmentService attachmentService;

	@SuppressWarnings("unused")
	@RequestMapping("/getUserName")
	@ResponseBody
	public void getUserName(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap<String, Object>();
		JSONObject obj = JSON.parseObject(Param);
		if (obj == null) {
			mapMarks.put("Code", InterfaceCode.PARAMETER_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.PARAMETER_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			return;
		}
		String Account = obj.getString("Account").trim();
		HashMap<String, Object> maps = null;
		HashMap<String, Object> mapMark = new HashMap<String, Object>();
		try {
			List<HashMap<String, Object>> map  = pigService.login(Account);

			if (map.size() == 0) {
				mapMarks.put("Code", InterfaceCode.USER_FAIL.getCode());
				mapMarks.put("Msg", InterfaceCode.USER_FAIL.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			}

			if (map.size() > 1) {
				mapMarks.put("Code", InterfaceCode.USER_REPEAT.getCode());
				mapMarks.put("Msg", InterfaceCode.USER_REPEAT.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			}

			if (map.get(0).get("error") != null) {
				mapMarks.put("Code", InterfaceCode.USER_LIMIT.getCode());
				mapMarks.put("Msg", InterfaceCode.USER_LIMIT.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			}

			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			mapMark.put("UserName", map.get(0).get("USERNAME"));
			mapMarks.put("Result", mapMark);
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);

		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unused")
	@RequestMapping("/loginApp")
	@ResponseBody
	public void login(String Param, HttpServletRequest request,
			HttpServletResponse rep) {

		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		String Account = obj.getString("Account").trim();
		String Code = obj.getString("Code");
		String DeviceID = obj.getString("DeviceID");
		Integer accessType = obj.getInteger("DeviceType");
		if (Account == null) {
			mapMarks.put("Code", InterfaceCode.PARAMETER_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.PARAMETER_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			return;
		}

		List<HashMap<String, Object>> map = null;
		HashMap<String, Object> mapMark = new HashMap<String, Object>();
		try {
			map = pigService.login(Account);
			if (map == null) {
				mapMarks.put("code", InterfaceCode.USER_FAIL.getCode());
				mapMarks.put("msg", InterfaceCode.USER_FAIL.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			}

			if (!Code.equals(map.get(0).get("PWD"))) {
				mapMarks.put("Code", InterfaceCode.CODE_FAIL.getCode());
				mapMarks.put("Msg", InterfaceCode.CODE_FAIL.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			}
			if (Code != null && Code.equals(map.get(0).get("PWD"))) {
				mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
				mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
				mapMark.put("UserID", map.get(0).get("ID_"));
				mapMarks.put("Result", mapMark);
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				request.getSession().setAttribute("App_User_", map);
			}

			String Id = UUID.randomUUID().toString().replaceAll("-", "");
			String userId = (String) map.get(0).get("ID_");
			int status = 0;
			pigService.insertLog(Id, userId, status, accessType);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			String Id = UUID.randomUUID().toString().replaceAll("-", "");
			String userId = (String) map.get(0).get("ID_");
			int status = 1;
			pigService.insertLog(Id, userId, status, accessType);
		}

	}

	@RequestMapping("/logoutApp")
	@ResponseBody
	public void getlogputApp(String Param, HttpServletRequest request,
			HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
		mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
		String map1 = JSON.toJSONString(mapMarks);
		AjaxResponse.sendText(rep, map1);
		request.getSession().removeAttribute("App_User_");
		// SecurityUtils.removeUser(request);
		// org.apache.shiro.SecurityUtils.getSubject().logout();

	}

	@RequestMapping("/getTasks")
	@ResponseBody
	public void getTasks(String Param, HttpServletResponse rep) {

		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		if (obj == null) {
			mapMarks.put("Code", InterfaceCode.PARAMETER_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.PARAMETER_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			return;
		}
		String UserID = obj.getString("UserID");
		this.checkUser(UserID, rep);
		BasicDataByFind find = new BasicDataByFind();
		if (null != obj.getInteger("PageNum")) {
			find.setCurrent(obj.getInteger("PageNum"));
		}
		if (null != obj.getInteger("PageSize")) {
			find.setPageCount(obj.getInteger("PageSize"));
		}
		int pageCount = 20;
		if (null != find.getCurrent()) {
			pageCount = find.getCurrent();
		}
		int current = 1;
		if (null != find.getPageCount()) {
			current = find.getPageCount();
		}
		PageHelper.startPage(pageCount, current);

		HashMap<String, Object> map = null;
		try {
			map = pigService.findTask();
			if (map != null && map.size() > 0) {
				mapMarks.put("Result", map);
			}
			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);

		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("/getTaskInfo")
	@ResponseBody
	public ModelAndView searchTask(String Param) {
		ModelAndView mav = new ModelAndView();
		JSONObject obj = JSON.parseObject(Param);
		String UserID = obj.getString("UserID");
		String taskId = obj.getString("TaskID");
		// this.checkUser(mav, rep);
		HashMap<String, Object> map = null;
		try {
			map = pigService.getTaskInfo(taskId);
			mav.addObject("Result", map);
			mav.addObject("Code", InterfaceCode.SUCCESS.getCode());
			mav.addObject("Msg", InterfaceCode.SUCCESS.getLabel());
		} catch (Exception e) {
			mav.addObject("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mav.addObject("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
		}

		return mav;
	}

	@RequestMapping("/searchTask")
	public ModelAndView searchTask(String UserID, String pipeName,
			Integer PageNum, Integer PageSize) {
		ModelAndView mav = new ModelAndView();
		// this.checkUser(mav,rep);
		BasicDataByFind find = new BasicDataByFind();
		find.setPageCount(PageSize);
		find.setCurrent(PageNum);
		int pageCount = 10;
		if (null != find.getPageCount()) {
			pageCount = find.getPageCount();
		}
		int current = 1;
		if (null != find.getCurrent()) {
			current = find.getCurrent();
		}
		PageHelper.startPage(current, pageCount);
		;
		Result result = null;
		try {
			// result = pigService.findMarker(pipeName);
			mav.addObject(result);
			mav.addObject("code", InterfaceCode.SUCCESS.getCode());
			mav.addObject("msg", InterfaceCode.SUCCESS.getLabel());
		} catch (Exception e) {
			mav.addObject("code", InterfaceCode.SERVICE_FAIL.getCode());
			mav.addObject("msg", InterfaceCode.SERVICE_FAIL.getLabel());
		}

		return mav;
	}

	@RequestMapping("/getPoints")
	@ResponseBody
	public void getTaskPoints(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		String UserID = obj.getString("UserID");
		String taskId = obj.getString("TaskID");
		this.checkUser(UserID, rep);
		HashMap<String, Object> map = null;
		try {
			map = pigService.findMarker(taskId);
			if (!map.isEmpty()) {
				mapMarks.put("Result", map);
			}
			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("/getTaskTypes")
	public ModelAndView getTaskTypes(String UserID) {
		ModelAndView mav = new ModelAndView();
		// this.checkUser(UserID,rep);
		Result result = null;
		try {
			result = pigService.getTaskTypes();
			mav.addObject(result);
			mav.addObject("Code", InterfaceCode.SUCCESS.getCode());
			mav.addObject("Msg", InterfaceCode.SUCCESS.getLabel());
		} catch (Exception e) {
			mav.addObject("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mav.addObject("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
		}

		return mav;
	}

	@RequestMapping("/getConfigs.json")
	@ResponseBody
	public void getConfigs(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		 String UserID = obj.getString("UserID");
		 this.checkUser(UserID,rep);
		HashMap<String, Object> map = new HashMap<String, Object>();
		try {
			map = pigService.getConfigs();
			if (map != null && map.size() > 0) {
				mapMarks.put("Result", map);
			}

			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	/**
	 * 获取发球点信息（skoda系统）
	 * 
	 * @param UserID
	 * @param TaskId
	 * @param SendID
	 * @param rep
	 */
	@RequestMapping("/getSendInfo")
	public void getSendInfo(String UserID, String TaskId, String SendID,
			HttpServletResponse rep) {
		ModelAndView mav = new ModelAndView();
		this.checkUser(UserID, rep);
		JSONObject object = new JSONObject();
		object.put("key", null);
		try {
			PrintWriter out = rep.getWriter();
			out.print(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// out.println("<html>");//输出的内容要放在body中
		// out.println("<body>");
		// out.println("要输入的内容.........");
		// out.println("</body>");
		// out.println("</html>");

	}

	@RequestMapping("/getBackInfo")
	public void getBackInfo(String UserID, String TaskId, String BackID,
			HttpServletResponse res) {
		JSONObject object = new JSONObject();
		object.put("key", null);
		try {
			PrintWriter out = res.getWriter();
			out.print(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/getMarkInfo")
	public void getBackInfo(String Param, HttpServletResponse res) {
		JSONObject object = new JSONObject();
		object.put("key", null);
		try {
			PrintWriter out = res.getWriter();
			out.print(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/putSendInfo")
	@ResponseBody
	public void putSendInfo(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		try {
			String UserID = obj.getString("UserID");
			this.checkUser(UserID, rep);
			String taskId = obj.getString("TaskID");
			String Sender = obj.getString("User");
			Date date = obj.getDate("Time");
			String remark = obj.getString("Remark");
			JSONArray picture = obj.getJSONArray("Pics");
			PigResults result = new PigResults();
			if (obj.getString("Flow") != null) {
				result.setSendBallsFlow(new BigDecimal(obj.getString("Flow")));
			}
			if (obj.getString("OutPressure") != null) {
				result.setSendBallsPressureFrom(new BigDecimal(obj
						.getString("OutPressure")));
			}
			if (obj.getString("InPressure") != null) {
				result.setSendBallsPressureTo(new BigDecimal(obj
						.getString("InPressure")));
			}
			if (obj.getString("PressureGap") != null) {
				result.setSendPressure(new BigDecimal(obj
						.getString("PressureGap")));
			}
			if (obj.getString("Interference") != null) {
				result.setSendInterference(new BigDecimal(obj
						.getString("Interference")));
			}
			if (obj.getString("PipeDiameter") != null) {
				result.setInterDiameter(new BigDecimal(obj
						.getString("PipeDiameter")));
			}
			int CleanType = obj.getIntValue("CleanType");
			String CleanTypeID = obj.getString("CleanTypeID");
			String CleanTool = obj.getString("CleanTool");
			String id = CommonUtil.getUUID();
			// 生成发球记录时间
			Date sendRecordtime = new Date();
			result.setSendRecordtime(sendRecordtime);
			result.setPiggngTaskId(taskId);
			result.setSendRecrder(Sender);
			result.setSendBallsTime(date);
			result.setSendRemark(remark);
			result.setPiggingType(CleanTypeID);
			if (CleanType == 1) {
				result.setPigType(CleanTool);
			} else {
				result.setDetectorType(CleanTool);
			}
			pigService.insertSendInfo(result);
			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("/putBackInfo")
	@ResponseBody
	public void putBackInfo(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		HashMap<String, Object> map = null;
		try {
			String UserID = obj.getString("UserID");
			this.checkUser(UserID, rep);
			String BackID = obj.getString("ID");
			String taskId = obj.getString("TaskID");
			String Backer = obj.getString("User");
			Date BackTime = obj.getDate("Time");
			String Remark = obj.getString("Remark");
			JSONArray picture = obj.getJSONArray("Pics");
			PigResults results = new PigResults();
			if (obj.getString("OutPressure") != null) {
				results.setTheBallsPressureFrom(new BigDecimal(obj
						.getString("OutPressure")));
			}
			if (obj.getString("InPressure") != null) {
				results.setTheBallsPressureTo(new BigDecimal(obj
						.getString("InPressure")));
			}
			if (obj.getString("Flow") != null) {
				results.setTheBallsFlow(new BigDecimal(obj.getString("Flow")));
			}
			if (obj.getString("MaxPressureGap") != null) {
				results.setMaxPushPressure(new BigDecimal(obj
						.getString("MaxPressureGap")));
			}
			if (obj.getString("DumpGas") != null) {
				results.setVentGasVolume(new BigDecimal(obj
						.getString("DumpGas")));
			}
			if (obj.getString("GasTemperature") != null) {
				results.setAvgTempreture(new BigDecimal(obj
						.getString("GasTemperature")));
			}
			if (obj.getString("GasDensity") != null) {
				results.setGasRelativedEnsity(new BigDecimal(obj
						.getString("GasDensity")));
			}
			if (obj.getString("PassGas") != null) {
				results.setActualPassGasVolume(new BigDecimal(obj
						.getString("PassGas")));
			}
			if (obj.getString("WasteWater") != null) {
				results.setSewage(new BigDecimal(obj.getString("WasteWater")));
			}
			if (obj.getString("Waste") != null) {
				results.setDirt(new BigDecimal(obj.getString("Waste")));
			}
			if (obj.getString("ToolMile") != null) {
				results.setPigRunLength(new BigDecimal(obj
						.getString("ToolMile")));
			}
			if (obj.getString("ToolSpeed") != null) {
				results.setAvgRate(new BigDecimal(obj.getString("ToolSpeed")));
			}
			if (obj.getString("Interference") != null) {
				results.setSendInterference(new BigDecimal(obj
						.getString("Interference")));
			}
			if (obj.getString("CompressFactor") != null) {
				results.setCompressionCoefficient(new BigDecimal(obj
						.getString("CompressFactor")));
			}
			String ToolRemark = obj.getString("ToolRemark");
			String DirtyRemark = obj.getString("DirtyRemark");
			int CleanType = obj.getIntValue("CleanType");
			String CleanTypeID = obj.getString("CleanTypeID");
			String CleanTool = obj.getString("CleanTool");

			// 生成收球记录时间
			Date recordtime = new Date();
			results.setRecordtime(recordtime);
			results.setPiggngTaskId(taskId);
			results.setBackBallsTime(BackTime);
			results.setRecrder(Backer);
			results.setSendRemark(Remark);
			results.setDirtdescRiption(DirtyRemark);
			results.setPiggingType(CleanTypeID);
			if (CleanType == 0) {
				mapMarks.put("Code", InterfaceCode.PARAMETER_FAIL.getCode());
				mapMarks.put("Msg", InterfaceCode.PARAMETER_FAIL.getLabel());
				String map1 = JSON.toJSONString(mapMarks);
				AjaxResponse.sendText(rep, map1);
				return;
			} else if (CleanType == 1) {
				results.setPigType(CleanTool);
			} else if (CleanType == 2) {
				results.setDetectorType(CleanTool);
			}
			// map=pigService.findSend(taskId);
			// if(map!=null &&map.size()>0 &&map.get("SENDTIME")==null){
			// mapMarks.put("Code", InterfaceCode.ORDER_FAIL.getCode());
			// mapMarks.put("Msg", InterfaceCode.ORDER_FAIL.getLabel());
			// String map1 = JSON.toJSONString(mapMarks);
			// AjaxResponse.sendText(rep, map1);
			// return;
			// }
			//
			pigService.updateBackInfo(results);
			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			e.printStackTrace();
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("passBall")
	@ResponseBody
	public void passBall(String Param, HttpServletResponse rep) {
		BigDecimal mile = null;
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		int type = obj.getIntValue("Type");
		String taskId = obj.getString("TaskID");
		String ID = obj.getString("ID");
		Date passTime = obj.getDate("Time");
		String UserID = obj.getString("UserID");
		if (obj.getString("Mile") != null) {
			mile = new BigDecimal(obj.getString("Mile"));
		}
		this.checkUser(UserID, rep);
		try {
			pigService.passBall(taskId, ID, passTime, mile, type);
			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("putMarkInfo")
	@ResponseBody
	public void postMarkInfo(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		BigDecimal mile = null;
		JSONObject obj = JSON.parseObject(Param);
		String MarkId = obj.getString("ID");
		String Marker = obj.getString("User");
		Date passtime = obj.getDate("Time");
		String UserID = obj.getString("UserID");
		this.checkUser(UserID, rep);
		String taskId = obj.getString("TaskID");
		Date arriveTime = obj.getDate("ArriveTime");
		String remark = obj.getString("Remark");
		JSONArray picture = obj.getJSONArray("Pics");
		if (obj.getString("Mile") != null) {
			mile = new BigDecimal(obj.getString("Mile"));
		}
		PigTaskPoint point = new PigTaskPoint();
		point.setId(MarkId);
		point.setArriveUser(Marker);
		point.setRecrder(Marker);
		point.setArriveTime(arriveTime);
		point.setPassTime(passtime);
		point.setMile(mile);
		point.setPiggngTaskId(taskId);
		point.setCreateRemark(remark);

		try {
			pigService.updateMarker(point);

			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	/**
	 * 人到达跟踪点接口
	 */
	@RequestMapping("arriveMark")
	@ResponseBody
	public void arriveMark(String Param, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		JSONObject obj = JSON.parseObject(Param);
		String taskId = obj.getString("TaskID");
		String ID = obj.getString("ID");
		Date time = obj.getDate("Time");
		String user = obj.getString("User");
		String UserID = obj.getString("UserID");
		this.checkUser(UserID, rep);
		PigTaskPoint point = new PigTaskPoint();
		point.setId(ID);
		point.setArriveTime(time);
		point.setPiggngTaskId(taskId);
		point.setArriveUser(user);
		point.setRecrder(user);
		try {
			pigService.arriveMark(point);

			mapMarks.put("Code", InterfaceCode.SUCCESS.getCode());
			mapMarks.put("Msg", InterfaceCode.SUCCESS.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		} catch (Exception e) {
			mapMarks.put("Code", InterfaceCode.SERVICE_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.SERVICE_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
		}

	}

	@RequestMapping("/upload")
	public void upload(@RequestParam MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) {

		Map param = RequestUtil.parseRequestParamToHashMap(request);

		StringBuffer result = new StringBuffer("{\"status\":");
		try {
			param = UploadUtil.load(param, file);
			String source = null;
			if ("1".equals(param.get("type"))) {
				source = "PIC_PIG_BALL_SEND";
			} else if ("2".equals(param.get("type"))) {
				source = "PIC_PIG_TASK_POINT";
			} else if ("3".equals(param.get("type"))) {
				source = "PIC_PIG_BALL_BACK";
			}
			param.put("source", source);
			attachmentService.updateInsert(param);
			String url = param.get("url").toString();
			String getRes = url;
			System.out.println(getRes);
			result.append("0,").append("\"id\":").append("\"")
					.append(param.get("id")).append("\"");
			result.append(",\"url\":").append("\"")
					.append(UploadUtil.SOURCE + getRes)

					.append("\"}");
			System.out.println(getRes);
		} catch (Exception e) {
			result.append("1,").append("\"err\":\"").append(e.getMessage())
					.append("\"}");
		}
		ResponseUtil.printl(response, result.toString(), "json");
	}

	private void checkUser(String UserID, HttpServletResponse rep) {
		Map mapMarks = new HashMap();
		if (UserID == null) {
			mapMarks.put("Code", InterfaceCode.PARAMETER_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.PARAMETER_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			return;
		}
		if (pigService.findUserId(UserID) == null) {
			mapMarks.put("Code", InterfaceCode.USER_FAIL.getCode());
			mapMarks.put("Msg", InterfaceCode.USER_FAIL.getLabel());
			String map1 = JSON.toJSONString(mapMarks);
			AjaxResponse.sendText(rep, map1);
			return;
		}
		return;
	}
}
